<?php
/**
 * Notes:
 * Author: chenjie
 * @return
 * Date: 2021/11/26
 */

namespace App\Repositories;


use App\Enums\ErmEnum;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class ErmBlackRemoveRepository
{

    public function getLog($param, $page, $perPage)
    {
        $ermBlackListRecordModel = app("model_erm_black_list_record");
        $ermPublicListModel = app("model_erm_public_list");
        $userDataModel = app('model_user_data');
        $adminModel = app('model_admin');

        $whereArr = [];
        if (isset($param['user_code'])) {
            $whereArr['t2.user_code'] = $param['user_code'];
        }
        if (isset($param['nickname'])) {
            $whereArr[] = ['t2.nickname', 'like', '%' . $param['nickname'] . '%'];
        }
        if (isset($param['user_type'])) {
            $whereArr['t1.user_type'] = $param['user_type'];
        }
        if (isset($param['start_register_time'])) {
            $param['start_register_time'] = strtotime($param['start_register_time']);
            $whereArr[] = ['t1.register_time', '>=', $param['start_register_time']];
        }
        if (isset($param['end_register_time'])) {
            $param['end_register_time'] = strtotime($param['end_register_time']);
            $whereArr[] = ['t1.register_time', '<=', $param['end_register_time']];
        }
        if(isset($param['admin_id']))
        {
            $whereArr['r.operator_id'] = $param['admin_id'];
        }
        if (isset($param['start_remove_time'])) {
            $param['start_remove_time'] = strtotime($param['start_remove_time']);
            $whereArr[] = ['r.create_time', '>=', $param['start_remove_time']];
        }
        if (isset($param['end_remove_time'])) {
            $param['end_remove_time'] = strtotime($param['end_remove_time']);
            $whereArr[] = ['r.create_time', '<=', $param['end_remove_time']];
        }

        $query = $ermBlackListRecordModel
            ->from($ermBlackListRecordModel->getTable() . ' as r')
            ->selectRaw('t2.nickname,t2.user_code,t1.user_type,t1.register_time,a.admin_name,r.*')
            ->join($ermPublicListModel->getTable() . ' as t1', 't1.user_id', '=', 'r.user_id')
            ->join($userDataModel->getTable() . ' as t2', 'r.user_id', '=', 't2.user_id')
            ->join($adminModel->getTable() . ' as a', 'r.operator_id', '=', 'a.admin_id')
            ->where($whereArr)
            ->orderBy("r.id", "desc");
        if($perPage)
        {
            $list = $query->paginate($perPage, ['*'], '', $page);
        }
        else
        {
            $list = $query->get();
        }

        foreach ($list as $item) {
            $item->user_type = ErmEnum::USER_TYPE[$item->user_type] ?? $item->user_type;
            $item->register_time = date('Y-m-d H:i:s', $item->register_time);
            $item->create_time = date('Y-m-d H:i:s', $item->create_time);
            if($item->recharge_prove)
            {
                $item->recharge_prove = json_decode($item->recharge_prove, true);
            }
            else
            {
                $item->recharge_prove = [];
            }
            if($item->relation_prove)
            {
                $item->relation_prove = json_decode($item->relation_prove, true);
            }
            else
            {
                $item->relation_prove = [];
            }
            if($item->other_prove)
            {
                $item->other_prove = json_decode($item->other_prove, true);
            }
            else
            {
                $item->other_prove = [];
            }
        }

        return $list;
    }

    public function remove($data)
    {
        //判断用户是否在黑名单
        $blackInfo = app('model_erm_black_list')->where(['user_id'=>$data['user_id']])->first();

        if(!$blackInfo)
        {
            throw new BasicException(10001, '用户不在黑名单中');
        }

        $data['join_time'] = $blackInfo['create_time'];
        $data['create_time'] = time();

        DB::beginTransaction();

        try {
            $info = app("model_erm_public_list")->where(['user_id' => $data['user_id']])->first();

            if(!app('model_erm_black_list_record')->insert($data))
            {
                throw new BasicException(10001, '黑名单移出记录添加失败');
            }

            app('repo_erm_public_list')->joinList([
                'id' => $info['id'],
                'old_type' => ErmEnum::BLACK_TYPE,
                'type' => ErmEnum::PUBLIC_TYPE,
                'admin_id' => $data['operator_id'],
            ]);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            throw new BasicException($e->getCode(), $e->getMessage());
        }
    }

}